﻿$(function () {


    var resultSelects = $(".resultSelect");

    //Creates all the divs for the result selection

    for (var i = 1; i <= resultSelects.length; i++) {
        $("#resultSelect" + i).html("<div style=\"margin-left:1%; width:25%; float:left\">" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"background-color:#A9F5BC; color:#FFFFFF\">0</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"h\">1</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"h\">2</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"h\">3</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"h\">4</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">5</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">6</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">7</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">8</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">9</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"h\" style=\"display:none\">10</div>" +
            "</div>" +
            "<div style=\" width:48%; margin-top:10px; float:left\">" +
                "<div id=\"bigResult" + i + "\" class=\"bigResult\">0-0</div>" +
                "<div class=\"buttonPronosticar\" onclick=\"closeResult(\'" + i + "\')\">Pronosticar</div>" +
                "<div id=\"verMasResultados" + i + "\" class=\"buttonVerMas\" onclick=\"togglePart2(\'" + i + "\')\">Más Resultados</div>" +
            "</div>" +
            "<div style=\"width:25%; float:left\">" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"background-color:#A9F5BC; color:#FFFFFF\">0</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"a\">1</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"a\">2</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"a\">3</div>" +
                "<div class=\"resultOption\" resultdiv=\"" + i + "\" hoa=\"a\">4</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">5</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">6</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">7</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">8</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">9</div>" +
                "<div class=\"resultOption secondaryResult\" resultdiv=\"" + i + "\" hoa=\"a\" style=\"display:none\">10</div>" +
            "</div>");
    }


    $(".resultOption").click(function () {

        var allOptions = $(this).parent().children();

        for (i = 0; i < $(this).parent().children().length; i++) {
            $(allOptions.get(i)).css("background-color", "");
            $(allOptions.get(i)).css("color", "");
        }

        $(this).css("background-color", "#A9F5BC");
        $(this).css("color", "#FFFFFF");

        var resultDiv = $(this).attr("resultdiv");

        var currentResult = $("#bigResult" + resultDiv).html();
        var newResult;
        var scores = currentResult.split("-");
        if ($(this).attr("hoa") == "h") {
            newResult = $(this).html() + "-" + scores[1];
        }
        else {
            newResult = scores[0] + "-" + $(this).html();
        }

        var bigResult = $("#bigResult" + resultDiv);

        bigResult.html(newResult);
        bigResult.css("background-color", "#FFFF66");
        bigResult.animate({ backgroundColor: '#FFFFFF' }, 400);
    });

    $(".resultField").click(function () {
        var resultSelect = $(this).attr("resultSelect");
        if ($("#algunResultAbierto").attr("value") == "false") {
            $("#resultSelect" + resultSelect).toggle("slow");
            $("#algunResultAbierto").attr("value", "true");
        }
        $(".secondaryResult").css("display", "none");
        $("#verMasResultados" + resultSelect).show();
        $("#resultSelect" + resultSelect).css("height", "190px");
    });

    $(".rowPosiciones").click(function () {
        var celdaUsuario = $(this).children().first().nextAll(".celdaIdUsuario");
        var idUsuario = $(celdaUsuario).html().trim();
        $("#idUsuario").val(idUsuario);
        $("#verPronostico").click();
    });

});

//Closes the div for the result selection
function closeResultSelect(id, resultado) {
    $("#resultSelect" + id).toggle("slow");
    $("[resultSelect='resultSelect" + id + "']").val(resultado);
    $("#algunResultAbierto").attr("value", "false");
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};